<?php
class coffee extends core {
	public function __construct() {
		parent::__construct();
	}

	//index
	public function index() {
		$where = '';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		if ($keyword) {
			$where .= "AND (name LIKE '%{$keyword}%' OR name_english LIKE '%{$keyword}%' OR memo LIKE '%{$keyword}%')";
		}
		if ($id) {
			$where .= "AND id='{$id}'";
		}
		//分页
		$this->setpages(array('id'=>$id, 'keyword'=>$keyword));
		$sql = "SELECT * FROM jf_coffee WHERE 1 {$where} ORDER BY id DESC";

		// $sql = "SELECT c.* , s.name as shop_name FROM jf_coffee_shop c LEFT JOIN jf_shop s ON c.shop_id = s.id 
		// 		WHERE 1 {$where} ORDER BY c.id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);

		$this->display();
	}

	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			// $sql = "SELECT * FROM jf_shop WHERE 1  ORDER BY id DESC";
			// $shop = $this->db->get_results($sql);
			// $this->smarty->assign('shop',$shop);
			$this->display();
		}
	}	

	//add
	public function edit() {
		if (IS_POST) { //添加
			$this->save();
		} else { //显示
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM jf_coffee WHERE id = '{$id}' ORDER BY id";
			$row = $this->db->get_row($sql);
			$sql = "SELECT * FROM jf_coffee_pic WHERE coffee_id = '{$id}'";
			$pics = $this->db->get_results($sql);
			$this->smarty->assign('pics', $pics);
			$this->smarty->assign('row', $row);
			$this->display();
		}
	}
		public function upload_pic(){
        global $domain;
        $file_result = upload_one_file('article','pic_file',true);
        $file_result = $file_result['file'];
        echo $file_result;
    }
	//写入到数据库
	public function save() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$price = (isset($_POST['price']) && trim($_POST['price'])) ? trim($_POST['price']) : '';
		$vip_price = (isset($_POST['vip_price']) && trim($_POST['vip_price'])) ? trim($_POST['vip_price']) : '';
		$market_price = (isset($_POST['market_price']) && trim($_POST['market_price'])) ? trim($_POST['market_price']) : 0;
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$sort = (isset($_POST['sort']) && intval($_POST['sort'])) ? intval($_POST['sort']) : 0;
		$sort_pic = (isset($_POST['sort_pic']) && intval($_POST['sort_pic'])) ? $_POST['sort_pic'] : '';
        $pics = (isset($_POST['pics']) && intval($_POST['pics'])) ? $_POST['pics']: '';
		$pic = upload_one_file("coffee","pic");
		if ($id > 0) {  //edit
			$sql = "UPDATE jf_coffee SET name='{$name}', price='{$price}' , vip_price='{$vip_price}',
					 market_price='{$market_price}', sort='{$sort}', status='{$status}'  
					 WHERE id='{$id}'";
			$act = $this->db->query($sql);
			//更新图片
			if($pic){
				$sql = "UPDATE jf_coffee SET default_pic='{$pic}' where id = '{$id}'";
				$this->db->query($sql);
			}
			//
			if($pics || $sort_pic){
                    $sql="DELETE FROM jf_coffee_pic WHERE coffee_id='{$id}'";
                    $this->db->query($sql);
                   	$sql="INSERT INTO jf_coffee_pic(coffee_id,pic,add_time,sort ,memo) VALUES ";
                    foreach($pics as $key => $pic){
                    	foreach ($sort_pic as $ks =>  $sort) {
                    		if($key == $ks){
                    			$sql.="('{$id}','{$pic}','".time()."','{$sort}','{$memo}') ,";
                    		}
                    	}
                    }
                    $sql=substr($sql,0,strlen($sql)-1);
                    $this->db->query($sql);
                }
		} else { //add
			$sql = "INSERT INTO jf_coffee(name, price ,vip_price,market_price,sort,default_pic,status,add_time) 
					VALUES('{$name}', '{$price}', '{$vip_price}','{$market_price}', 
					'{$sort}', '{$pic}','{$status}', '".time()."')";
			$this->db->query($sql);
			$insert_id = $this->db->insert_id;
			if($insert_id && $pics){
                    $sql="INSERT INTO jf_coffee_pic (coffee_id,pic,add_time,sort) VALUES ";
                    foreach($pics as $key => $pic){
                    	foreach ($sort_pic as $ks =>  $sort) {
                    		if($key == $ks){
                    			$sql.=" ('{$insert_id}','{$pic}','".time()."','{$sort}'),";
                    		}
                    	}
                    }
                    $sql=substr($sql,0,strlen($sql)-1);
                    $this->db->query($sql);
                }
		}
		header("Location:?app=coffee&act=index");
	}
	public function order_index(){
		$where = '';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		if ($keyword) {
			$where .= "AND (name LIKE '%{$keyword}%' OR name_english LIKE '%{$keyword}%' OR memo LIKE '%{$keyword}%')";
		}
		if ($id) {
			$where .= "AND id='{$id}'";
		}
		//分页
		$this->setpages(array('id'=>$id, 'keyword'=>$keyword));
		$sql = "SELECT o.*, s.name as coffee_shop_name, m.name as member_name FROM jf_coffee_order o LEFT JOIN jf_coffee_shop s ON o.coffee_shop_id = s.id 
				LEFT JOIN jf_member m ON o.member_id = m.id  
		  		WHERE 1 {$where} ORDER BY o.id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		$this->display();
	}
	public function order_edit(){
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
		if(IS_POST){
			// $total_price = (isset($_POST['total_price']) && trim($_POST['total_price'])) ? trim($_POST['total_price']) : '';
			// $discount_price = (isset($_POST['discount_price']) && trim($_POST['discount_price'])) ? trim($_POST['discount_price']) : '';
			// $coffee_id = (isset($_POST['coffee_id']) && trim($_POST['coffee_id'])) ? trim($_POST['coffee_id']) : '';
			// $price = (isset($_POST['price']) && trim($_POST['price'])) ? trim($_POST['price']) : '';
			// $quantity = (isset($_POST['quantity']) && trim($_POST['quantity'])) ? trim($_POST['quantity']) : '';
			// $sql = "SELECT jf_coffee_order SET total_price='{$total_price}' , discount_price= '{$discount_price}' 
			// 		WHERE id = {$id}";
			// $this->db->query($sql);
			// $thing ='';
			// foreach ($coffee_id as $key => $val) {
			// 	foreach ($price as $ke => $va) {
			// 		foreach ($quantity as $k => $v) {
			// 			if($k == $ke){
							
			// 			}
			// 		}
			// 	  }
			// 	}
				
			// $sql = "SELECT jf_coffee_order_coffee SET price='{$price}' , quantity= '{$quantity}' 
			// 		WHERE order_id = {$id}";
			// $this->db->query($sql);

		}else{
		$sql = "SELECT o.*, s.name as coffee_shop_name, m.name as member_name ,p.name as shop_name 
				FROM jf_coffee_order o LEFT JOIN jf_coffee_shop s ON o.coffee_shop_id = s.id 
				LEFT JOIN jf_member m ON o.member_id = m.id  LEFT JOIN jf_shop p ON o.shop_id = p.id
		  		WHERE 1  ORDER BY o.id DESC";
		$row = $this->db->get_row($sql);

		$sql ="SELECT c.*,m.name as member FROM jf_coffee_order_coffee c  
			LEFT JOIN jf_member m ON c.member_id = m.id WHERE order_id = {$id} ORDER BY c.id DESC";
		$coffee = $this->db->get_results($sql);
		$this->smarty->assign('coffee',$coffee);
		$this->smarty->assign('row',$row);
		$this->display();
	   }
	}


	//delete
	public function delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM jf_coffee WHERE id='{$id}'";
		$this->db->query($sql);
		$sql = "DELETE FROM jf_coffee_pic WHERE coffee_id='{$id}'";
		$this->db->query($sql);
		header("Location:?app=coffee&act=index");
	}
	public function delete_coffee_order() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM jf_coffee_order WHERE id='{$id}'";
		$this->db->query($sql);
		$sql = "DELETE FROM jf_coffee_order_coffee WHERE order_id='{$id}'";
		$this->db->query($sql);
		header("Location:?app=coffee&act=order_index");
	}
}
